/**
 * Created by oneminute on 7/27/14.
 */

var assert = require("assert");
var should = require("should");
var logger = require("../../lib/utils/logger");
var testutils = require("../testutils");
var lz = require("../../lib/utils/modules")();
var httpMocks = require("../lib/http-mock");
var async = require("async");
var route = require("../../routes/route");
var initDatas = require("../../lib/tools/initdatas");
var moment = require("moment");


describe("测试routes/search模块。", function () {

    var express = httpMocks.createExpress();
    var users = [];

    var initTestingDatas = function (_callback) {
        lz.Logger.debug("开始初始化测试数据到缓存。");
        var poses = [
            new initDatas.HotPosition({
                fixCount: true,
                count: 20,
                radius: 300,
                fn: initDatas.rand,
                location: {
                    lon: 117.118489,
                    lat: 36.694713
                }
            })
        ];
        var i = 0;
        var tasks = [];
        poses.forEach(function (_pos) {
            _pos.generateSamples(function (_hotPos, _loc) {
                var uid = lz.Utils.getUUID("u_");
                var name = ("" + (1000000 + i++)).substring(1);
                var password = name;
                var commodities = [];
                var demands = [];
                for (var j = 0; j < 5; j++) {
                    var commodity = {
                        id: lz.Utils.getUUID("c_"),
                        uid: uid,
                        description: "debug_commodity_测试描述" + j,
                        price: Math.random() * 500,
                        photos: ["7a66aec0f2e111e38d4239a1595329eb.png", "7a66aec0f2e111e38d4239a1595329eb.png", "7a66aec0f2e111e38d4239a1595329eb.png", "7a66aec0f2e111e38d4239a1595329eb.png"],
                        publishDate: moment().format(lz.Config.datetimeFormatLong)
                    };

                    var demand = {
                        id: lz.Utils.getUUID("d_"),
                        uid: uid,
                        description: "debug_demand_测试描述" + j,
                        publishDate: moment().format(lz.Config.datetimeFormat),
                        expireDate: moment().format(lz.Config.datetimeFormat),
                        publishDate: moment().format(lz.Config.datetimeFormat)
                    };
                    commodities.push(commodity);
                    demands.push(demand);
                }
                var user = new lz.User({
                    id: uid,
                    name: name,
                    password: password,
                    lon: _loc.lon,
                    lat: _loc.lat,
                    phone: "18652163487",
                    photo: "4f93be80f4fe11e38d4239a1595329eb.png",
                    address: "测试位置",
                    description: "这是一段测试描述文本这是一段测试描述文本这是一段测试描述文本这是一段测试描述文本这是一段测试描述文本",
                    inbusiness: true,
                    email: {
                        email: name + "@lz.com",
                        uid: uid,
                        authenticated: false,
                        authtime: moment().format(lz.Config.datetimeFormatLong),
                        authcode: ""
                    },
                    accounts: [
                        {
                            id: lz.Utils.getUUID("b_"),
                            uid: uid,
                            account: "q_" + name,
                            publisher: "qq"
                        },
                        {
                            id: lz.Utils.getUUID("b_"),
                            uid: uid,
                            account: "w_" + name,
                            publisher: "weixi"
                        }
                    ],
                    commodities: commodities,
                    demands: demands
                });

                tasks.push(function (_cb) {
                    user.save(function (_err, _user) {
                        if (_err) {
                            lz.Logger.error(_err);
                        }
                        users.push(_user);
                        _cb(_err);
                    });
                });
            });
        });

        async.series(tasks, function () {
            lz.Logger.info("generate testing datas completed.");
            _callback(null);
        });
    };

    before(function (done) {
        route.setup(express);
        initTestingDatas(function () {
            done();
        });
        //done();
    });

    it("正常搜索周边。", function (done) {
        var request = httpMocks.createExpressRequest({
            method: 'GET',
            url: '/search?lat=36.694713&lon=117.118489&type=commodities&p=0&count=5'
        });
        var response = httpMocks.createExpressResponse();

        express.invoke(request, response, function (_data) {
            testutils.check(done, function () {
                assert.equal(_data.array.length >= 1, true);
            });
        });
    });

    after(function (done) {
        var tasks = [];
        users.forEach(function (_user) {
            tasks.push(function (_cb) {
                _user.delete(function () {
                    _cb(null);
                });
            });
        });
        async.series(tasks, function () {
            done();
        });
    });
});